package com.office.supplies.controller;

import com.github.pagehelper.PageInfo;
import com.office.supplies.pojo.Result;
import com.office.supplies.pojo.Sale;
import com.office.supplies.pojo.dto.SelectSaleListDTO;
import com.office.supplies.pojo.vo.SaleGoodsVO;
import com.office.supplies.pojo.vo.SaleListVO;
import com.office.supplies.service.SaleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author admin
 * @Description TODO
 * @Date 2023/12/12/12:51
 */
@RestController
@Slf4j
@RequestMapping("/sale")
public class SaleController {

    @Autowired
    private SaleService saleService;

    /**
     * 获取所有商品信息和对应的库存
     *
     * @return Result<SaleGoodsVO>
     */
    @GetMapping("/getGoods")
    public Result<List<SaleGoodsVO>> getGoodsListForSale(){
        log.info("获取所有商品信息和对应的库存... ...");
        return Result.success(saleService.getGoodsListForSale());
    }

    /**
     * 新增出库申请
     *
     * @param sale Sale
     * @return Result<String>
     */
    @PostMapping
    public Result<String> sale(@RequestBody Sale sale){
        log.info("新增出库申请:{}",sale);
        saleService.saleGoods(sale);
        return Result.success();
    }

    @GetMapping("/pass/{id}")
    public Result<String> pass(@PathVariable Long id){
        log.info("审批出库申请:{}",id);
        saleService.pass(id);
        return Result.success("出库审批成功！");
    }

    /**
     * 查询销售订单
     *
     * @param pageNum Integer
     * @param pageSize Integer
     * @param selectSaleListDTO SelectSaleListDTO
     * @return Result<PageInfo<SaleListVO>>
     */
    @PostMapping("/list/{pageNum}/{pageSize}")
    public Result<PageInfo<SaleListVO>> getGoodsList(@PathVariable("pageNum") Integer pageNum,
                                                     @PathVariable("pageSize") Integer pageSize,
                                                     @RequestBody SelectSaleListDTO selectSaleListDTO){
        log.info("查询销售订单：{}，{}，{}",pageNum,pageSize,selectSaleListDTO);
        return Result.success(saleService.getSaleListByPageAndCondtion(pageNum,pageSize,selectSaleListDTO));
    }

    /**
     * 修改销售订单
     *
     * @param sale Sale
     * @return Result<String>
     */
    @PutMapping("/update_sale_info")
    public Result<String> updateBuyInfo(@RequestBody Sale sale){
        log.info("修改销售订单:{}",sale);
        saleService.updateById(sale);
        return Result.success("修改成功！");
    }

    /**
     * 删除销售信息
     *
     * @param id Long
     * @return Result<String>
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteBuyInfo(@PathVariable Long id){
        log.info("删除销售信息:{}",id);
        saleService.deleteSaleInfo(id);
        return Result.success();
    }


    /**
     * 删除销售信息
     *
     * @param id Long
     * @return Result<String>
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteBuyById(@PathVariable Long id){
        log.info("直接删除销售信息:{}",id);
        saleService.deleteSaleById(id);
        return Result.success();
    }


}
